package com.archerdu.base.service;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.List;

import net.sf.json.JSONArray;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.Order;

import com.archerdu.base.dao.BaseEntity;
import com.archerdu.base.dao.IBaseDao;
import com.archerdu.base.dao.IPage;
import com.archerdu.base.dao.TreeNode;

/**
 * 业务层基类，实现公用方法
 * 
 * @projectName springDemo
 * @description 业务层基类，实现公用方法
 * @author archer du
 * @createDate 2013年9月27日 下午5:26:15
 * @updateUser dxf
 * @updateDate 2013年9月27日 下午5:26:15
 * @updateRemark 新建
 * @version 0.0.1
 */
public abstract class BaseManager<E extends BaseEntity<K>, K extends Serializable> implements IBaseManager<E, K> {

    private static final long serialVersionUID = 6413456557120433534L;

    protected final Log       log              = LogFactory.getLog(getClass());

    protected abstract IBaseDao<E, K> getDao();

    @Override
    public List<E> getAll(E entity) {
        return getDao().findAll(entity);
    }

    @Override
    public List<E> getAll(E entity, Order order) {
        return getDao().findAll(entity, order);
    }

    public IPage<E> pageSearch(E entity, int pageNo, int pageSize) {
        return getDao().pageSearch(entity, pageNo, pageSize);
    }

    /**
     * 获取时间戳
     * 
     * @return
     */
    protected Timestamp getSqlDate() {
        return new Timestamp(System.currentTimeMillis());
    }

    public void add(E entity) throws Exception {
        getDao().save(entity);
    }

    public void modify(E entity) throws Exception {
        getDao().update(entity);
    }

    @Override
    public void modifyByMerge(E entity) throws Exception {
        getDao().merge(entity);
    }

    public void delete(E entity) throws Exception {
        getDao().delete(entity);
    }

    public void deleteByKey(K id) {
        getDao().deleteByKey(id);
    }

    public E get(K id) {
        return getDao().get(id);
    }

    public List<E> getAll() {
        return getDao().findAll();
    }

    /**
     * 加载子节点
     * 
     * @param region
     */
    @SuppressWarnings("unchecked")
    public void loadChildren(TreeNode<E, K> entity) {
        entity.setChildren(getDao().findByParentId(entity.getId()));

        if (null != entity.getChildren() && entity.getChildren().size() > 0) {
            for (E en : entity.getChildren()) {
                loadChildren((TreeNode<E, K>) en);
            }
        } else {
            return;
        }
    }

    /**
     * 异步加载子节点，每次之加载一层
     * 
     * @param region
     */
    public void loadAsynChildren(TreeNode<E, K> entity) {
        entity.setChildren(getDao().findByParentId(entity.getId()));
    }

    /**
     * 加载Json字符串
     * 
     * @param entity
     * @return
     */
    public String loadJson(TreeNode<E, K> entity) {
        if (null == entity) {
            log.info("目录对象不存在");
            return "";
        }
        log.info("#--- 父类id值： " + entity.getId());
        loadChildren(entity);
        JSONArray jsonObject = JSONArray.fromObject(entity.getChildren());

        return jsonObject.toString();
    }

    /**
     * 加载Json对象
     * 
     * @param entity
     * @return
     */
    public JSONArray loadJsonObject(TreeNode<E, K> entity) {
        if (null == entity) {
            log.info("目录对象不存在");
            return JSONArray.fromObject("");
        }
        log.info("#--- 父类id值： " + entity.getId());
        loadChildren(entity);

        return JSONArray.fromObject(entity.getChildren());
    }

    @Override
    public List<E> findByParentId(K key) {
        return getDao().findByParentId(key);
    }

    /**
     * 异步加载Json字符串，每次加载一层
     * 
     * @param entity
     * @return
     */
    public String loadAsynJson(TreeNode<E, K> entity) {
        log.info("#--- 父类id值： " + entity.getId());
        loadAsynChildren(entity);
        JSONArray jsonObject = JSONArray.fromObject(entity.getChildren());

        return jsonObject.toString();
    }
}
